package eyrolles;

import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import modele.Employe;
import modele.EmployeDB;
import modele.InterfaceEmploye;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionMessage;

public class AddEmployeAction extends ActionDB {

    protected void insertUser(ActionForm form)
            throws SQLException {

        try {

            EmployeForm eForm = (EmployeForm) form;
            conn = getConnection();

            // Récupération des informations du formulaire
            Employe employe = new Employe();            
            employe.setName(eForm.getName());
            employe.setPhone(eForm.getPhone());
            employe.setDepid(Integer.parseInt(eForm.getDepid()));
            employe.setRoleid(Integer.parseInt(eForm.getRoleid()));
            employe.setEmail(eForm.getEmail());
            employe.setUsername(eForm.getUsername());
            employe.setPassword(eForm.getPassword());
            
            InterfaceEmploye employeDB = new EmployeDB();
            employeDB.connect(conn);
            employeDB.createEmploye(employe);
            
        } catch (NullPointerException e) {

            throw new SQLException();
        } finally {

            freeConnection(conn);
        }
    }

    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException {

        ActionMessages errors = new ActionMessages();

        // Cible par defaut en cas de succes
        String target = new String("success");

        // teste l'identification de l'utilisateur    
        HttpSession session = request.getSession();
        if (session.getAttribute("USER") == null) {

            // L'utilisateur n'est pas identifie
            target = new String("login");

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.login.required"));

            // Signalement des erreurs eventuelles
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }

            // Si jamais l'utilisateur n'est pas loggé, alors
            // on retourne directement à la page de login
            // avant d'effectuer la moindre requête    
            return (mapping.findForward(target));
        }

        if (isCancelled(request)) {

            // Annulation. Retour a la liste des employes
            return (mapping.findForward(target));
        }

        try {

            insertUser(form);
        } catch (SQLException e) {

            target = new String("error");
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.database.error"));
        }

        // Signalement des erreurs eventuelles
        if (!errors.isEmpty()) {

            saveErrors(request, errors);
        }

        // Transmission a la vue appropriee
        return (mapping.findForward(target));
    }
}